package cn.thornbird.orgsync.model.netease;

import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.util.EncryptUtil;
import cn.thornbird.orgsync.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class AccountInfo extends AccountList.Account {

    public static final String CREATE_FIELDS = "accountName,domain,name,password,activesync,bindMobile," +
            "bindMobilePrefix,birthday,createEmail,cardNum,cardType,empTime,enableAccessAddr,enableCloudAttachment," +
            "enableCommonResetPass,enableEmailMasterProtocol,enableFwd,enableMobileResetPass,expiredTime,fax," +
            "fwdMAuth,gender,job,jobNumber,mailArchive,mailNotary,mobile,passChangeFirstLogin,passport,passType," +
            "remark,secondaryLogon,sirius,state,tel,unitId,visibleInAddr";

    public static final String UPDATE_FIELDS = "accountName,domain,activesync,bindMobile,bindMobilePrefix,birthday," +
            "cardNum,cardType,displayEmail,empTime,enableAccessAddr,enableCloudAttachment,enableCommonResetPass," +
            "enableEmailMasterProtocol,enableFwd,enableMobileResetPass,expiredTime,fax,fwdMAuth,gender,job,jobNumber," +
            "mailArchive,mailNotary,mobile,name,passChangeFirstLogin,passport,popDelete,remark,secondaryLogon,sirius," +
            "state,tel,unitId,visibleInAddr,enableAuthCode";

    public static final String UPDATE_PASSWORD_FIELDS = "accountName,domain,password,passType,passChangeFirstLogin";

    public static final String MOVE_UNIT_FIELDS = "accountName,domain,unitId";

    /**
     * 是否创建邮箱: 1-创建, 0-不创建 (默认1)
     */
    @JsonProperty("createEmail")
    private String createEmail;

    /**
     * 通讯录账号显示的邮箱地址, 主账号邮箱或者是别名邮箱地址
     */
    @JsonProperty("displayEmail")
    private String displayEmail;

    /**
     * 用于账户安全的绑定手机号区号 (默认86)
     */
    @JsonProperty("bindMobilePrefix")
    private String bindMobilePrefix;

    /**
     * 国内证件号 (当state值为"中国 China"时传值) 该字段支持空值
     */
    @JsonProperty("cardNum")
    private String cardNum;

    /**
     * 国内证件类型 (当state值为"中国 China"时传该值, 默认值为0)
     * 0-身份证号, 1-港澳通行证, 2-台湾通行证, 3-中国护照
     */
    @JsonProperty("cardType")
    private Integer cardType;

    /**
     * 是否可以使用云附件: 1-可以, 0-不可以 (默认可以)
     */
    @JsonProperty("enableCloudAttachment")
    private Integer enableCloudAttachment;

    /**
     * 是否可以使用邮箱大师私有协议: 1-可以, 0-不可以 (默认可以)
     */
    @JsonProperty("enableEmailMasterProtocol")
    private Integer enableEmailMasterProtocol;

    /**
     * 外国护照号 (当state值非"中国 China"时传值) 该字段支持空值
     */
    @JsonProperty("passport")
    private String passport;

    /**
     * 密码类型, 密码是否md5编码过: 0-明文, 1-经过md5的hex编码
     */
    @JsonProperty("passType")
    private Integer passType;

    /**
     * 密码
     */
    @JsonProperty("password")
    private String password;

    /**
     * 国籍, 传值参考最后页备注国家值, 不传默认值为"中国 China"
     */
    @JsonProperty("state")
    private String state;

    /**
     * 强制设置客户端授权码: 0-关闭, 1-强制开启, 2-开启, 3-强制关闭
     */
    @JsonProperty("enableAuthCode")
    private Integer enableAuthCode;

    public Map<String, Object> toCreateParams() {
        return toParams(CREATE_FIELDS);
    }

    public Map<String, Object> toUpdateParams() {
        return toParams(UPDATE_FIELDS);
    }

    public Map<String, Object> toUpdatePasswordParams() {
        return toParams(UPDATE_PASSWORD_FIELDS);
    }

    public Map<String, Object> toMoveUnitParams() {
        return toParams(MOVE_UNIT_FIELDS);
    }

    public Map<String, Object> toParams(String fields) {
        return JsonUtil.toMap(this, StringUtils.isEmpty(fields) ? null : fields.split(","));
    }

    public AccountInfo setId(String id) {
        setAccountName(id);
        return this;
    }

    public AccountInfo setDepartments(Map<String, AppUserData.DepartmentInfo> departments, String mainDepartmentId) {
        if (departments != null && !departments.isEmpty()) {
            setUnitList(new ArrayList<>());
            for (Map.Entry<String, AppUserData.DepartmentInfo> entry: departments.entrySet()) {
                getUnitList().add(entry.getKey());
            }
            if (!getUnitList().isEmpty()) {
                setUnitId(String.join(",", getUnitList()));
            }
        }
        return this;
    }

    public AccountInfo setLeaderId(String leaderId) {
        return this;
    }

    public AccountInfo setDefaultPassword(String password) {
        setPassword(EncryptUtil.md5(password));
        setPassType(1);
        return this;
    }

}
